package com.waimai.controllor.ele;

import java.util.ArrayList;

import org.htmlparser.Node;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.util.NodeList;

import com.waimai.controllor.Interface.IHtmlControllor;
import com.waimai.utils.HtmlParserUtils;
import com.waimai.utils.StringUtils;

public class EleHtmlControllor implements IHtmlControllor {

	private static EleHtmlControllor controllor;

	public static EleHtmlControllor getInstance() {
		if (controllor == null) {
			controllor = new EleHtmlControllor();
		}
		return controllor;
	}

	@Override
	public String getName(Node n) {
		// TODO Auto-generated method stub
		String name = "";
		Node nameLink = HtmlParserUtils.getChildNode(n, new HasAttributeFilter(
				"style", "font-size:15px; margin-left:10px;"));
		if (nameLink != null) {
			name = nameLink.toPlainTextString();
		}
		return name;
	}

	@Override
	public String getHrefUrl(Node n) {
		// TODO Auto-generated method stub
		String href = "";
		Node hrefNode = HtmlParserUtils.getChildNode(n, new HasAttributeFilter(
				"class", "restaurant-link"));
		if (hrefNode != null) {
			href = StringUtils.pattern(hrefNode.getText(), "me/[^ ]*");
			href = href.substring(2, href.length() - 1);
		}
		return href;
	}

	@Override
	public ArrayList<String> getDrink(Node n) {
		// TODO Auto-generated method stub
		// img class="restaurant-icons" +
		ArrayList<String> list = new ArrayList<String>();
		Node iconsNode = HtmlParserUtils.getChildNode(n,
				new HasAttributeFilter("class", "icons"));
		if (iconsNode != null) {
			NodeList drinkList = HtmlParserUtils.getChildrenNodeList(iconsNode,
					new HasAttributeFilter("img"));
			for (int i = 0; i < drinkList.size(); i++) {
				Node drinkNode = drinkList.elementAt(i);
				String drinkText = drinkNode.getText();
				String drink;
				if (drinkText.contains("汇源e果萃蓝莓果汁200ml一盒")) {
					drink = "汇源e果萃蓝莓果汁200ml一盒";
					list.add(drink);
				}
				if (drinkText.contains("免费赠饮")) {
					drink = "免费赠饮";
					list.add(drink);
				}
				if (drinkText.contains("加1元得可得康师傅果汁")) {
					drink = "加1元得可得康师傅果汁";
					list.add(drink);
				}
			}
		}
		return list;
	}

	@Override
	public int getCompany() {
		// TODO Auto-generated method stub
		return 1;
	}

	@Override
	public int getMaxBack(Node n) {
		// TODO Auto-generated method stub
		int maxBack = 0;
		Node maxBackNode = HtmlParserUtils
				.getChildNode(n, new HasAttributeFilter("class",
						"restaurant-icons get-hongbao"));
		if (maxBackNode != null) {
			String back = StringUtils.pattern(maxBackNode.getText(), "最高.*元红包");
			back = back.substring(2, back.length() - 3);
			maxBack = Integer.parseInt(back);
		}
		return maxBack;
	}

	@Override
	public int getStatus(Node n) {
		// TODO Auto-generated method stub
		int status = 0;
		Node statusNode = HtmlParserUtils.getChildNode(n,
				new HasAttributeFilter("class", "status-label closed"));
		if (statusNode == null) {
			status = 1;
		}
		return status;
	}

	@Override
	public String getImgUrl(Node n) {
		// TODO Auto-generated method stub
		String ImgUrl = "";
		Node ImgUrlNode = HtmlParserUtils.getChildNode(n,
				new HasAttributeFilter("class", "restaurant-logo"));
		if (ImgUrlNode != null) {
			ImgUrl = StringUtils.pattern(ImgUrlNode.getText(), "http://[^?]*");
		}
		return ImgUrl;
	}

	@Override
	public String getTotalPerMon(Node n) {
		// TODO Auto-generated method stub
		String perMon = "";
		Node permonNode = HtmlParserUtils.getChildNode(n,
				new HasAttributeFilter("class", "rating-number"));
		if (permonNode != null) {
			perMon = StringUtils.getIntStr(permonNode.toPlainTextString())
					+ "份";
		}
		return perMon;
	}

	@Override
	public String getSendTime(Node n) {
		// TODO Auto-generated method stub
		String sendTime = "";
		Node sendTimeNode = null;
		// 获得显示送餐时间
		sendTimeNode = HtmlParserUtils.getChildNode(n, new HasAttributeFilter(
				"class", "deliver-time-wrapper free tooltip_on"));
		if (sendTimeNode != null) {
			sendTime = StringUtils.replaceBlank(sendTimeNode
					.toPlainTextString());
		} else {
			// 获得忙时送餐时间
			sendTimeNode = HtmlParserUtils.getChildNode(n,
					new HasAttributeFilter("class",
							"deliver-time-wrapper busy tooltip_on"));
			if (sendTimeNode != null) {
				sendTime = StringUtils.replaceBlank(sendTimeNode
						.toPlainTextString());
			} else {
				// 获得餐厅平均送餐时间
				sendTimeNode = HtmlParserUtils.getChildNode(n,
						new HasAttributeFilter("class",
								"deliver-time-wrapper tooltip_on"));
			}
		}
		if (sendTimeNode != null) {
			sendTime = StringUtils.pattern(sendTimeNode.getText(), "[0-9]分钟");
		}
		return sendTime;
	}

	@Override
	public String getStartPrice(Node n) {
		// TODO Auto-generated method stub
		String StartPrice = "";
		Node StartPriceNode = HtmlParserUtils.getChildNode(n,
				new HasAttributeFilter("class", "restaurant-deliver-desc"));
		if (StartPriceNode != null) {
			StartPrice = StringUtils.pattern(
					StartPriceNode.toPlainTextString(), "[0-9]*元");
		}
		return StartPrice;
	}

	@Override
	public String getSendPrice(Node n) {
		// TODO Auto-generated method stub
		String sendPrice = "";
		Node sendPriceNode = HtmlParserUtils
				.getChildNode(n, new HasAttributeFilter("class",
						"restaurant-icons deliver-fee"));
		if (sendPriceNode != null) {
			sendPrice = StringUtils.pattern(sendPriceNode.getText(), "[0-9]*元");
		}
		return sendPrice;
	}

}
